package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.common.util.LogicException;
import com.alibaba.fastjson.JSON;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.InitBinder;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 猫教授
 * @date 2019/8/8 0:05
 *
 */
@ControllerAdvice
public class ExceptionAdviceController {
    //统一处理自定义异常
    @ExceptionHandler(LogicException.class)
    public void LogicException(Exception e, HttpServletResponse response) throws IOException {
        //首先我们开发阶段需要打印异常信息所以这时候要把异常信息先打印出来
        e.printStackTrace();
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().print(JSON.toJSONString(new AjaxResult(false,e.getMessage())));
    }
    //统一处理系统运行异常
    @ExceptionHandler(RuntimeException.class)
    public void RuntimeException(Exception e, HttpServletResponse response) throws IOException {
        //首先我们开发阶段需要打印异常信息所以这时候要把异常信息先打印出来
        e.printStackTrace();
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().print(JSON.toJSONString(new AjaxResult(false,"网络故障,请稍后再试或者联系管理员")));
    }
}
